#include "passwordmanagerwindow.h"
#include "ui_passwordmanagerwindow.h"

PasswordManagerWindow::PasswordManagerWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PasswordManagerWindow)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground);

    // 监听列表点击事件
    connect(this->ui->listWidget, &QListWidget::itemClicked, this, &PasswordManagerWindow::updateRightContent);
}

PasswordManagerWindow::~PasswordManagerWindow()
{
    delete ui;
}

void PasswordManagerWindow::paintEvent(QPaintEvent *event)
{
    // 移动至父窗口往下20
    QWidget *pa = dynamic_cast<QWidget*>(parent());
    QPoint p = pa->pos();
    QSize s = pa->size();
    move(s.width() / 2 - size().width() / 2, s.height() / 5);
}

void PasswordManagerWindow::addHostInfo(AccountInfo info)
{
    infos.push_back(info);
    QListWidgetItem *item = new QListWidgetItem();
    AccountItem* accountItem = new AccountItem();
    accountItem->info = info;
    accountItem->updateData();
    item->setSizeHint(accountItem->size());
    this->ui->listWidget->addItem(item);
    this->ui->listWidget->setItemWidget(item, accountItem);
    resetData();
    resetInputData();
}

void PasswordManagerWindow::addHostInfos(std::vector<AccountInfo> infos)
{

}

void PasswordManagerWindow::deleteHostInfo(AccountInfo info)
{

}

void PasswordManagerWindow::resetData()
{
    this->ui->username->setText("");
    this->ui->password->setText("");
    this->ui->host->setText("");
}

void PasswordManagerWindow::resetInputData()
{
    this->ui->input_password->clear();
    this->ui->input_domain->clear();
    this->ui->input_username->clear();
}

void PasswordManagerWindow::updateRightContent(QListWidgetItem *item)
{
    QListWidget *widgets = item->listWidget();
    AccountItem *accountItem = qobject_cast<AccountItem*>(widgets->itemWidget(item));
    AccountInfo accountInfo = accountItem->info;
    this->ui->password->setText(QString(accountInfo.password.c_str()));
    this->ui->username->setText(QString(accountInfo.username.c_str()));
    this->ui->host->setText(QString(accountInfo.host.c_str()));
    this->ui->host_url->setText("https://" + QString(accountInfo.host.c_str()));
}

void PasswordManagerWindow::loadAccountInfo()
{
    // 加载数据
    // 刷新列表控件
    this->ui->listWidget->clear();
    for(int i = 0; i < infos.size(); i++){
        QListWidgetItem *item = new QListWidgetItem();
        AccountItem* accountItem = new AccountItem();
        accountItem->info = infos[i];
        item->setSizeHint(accountItem->size());
        this->ui->listWidget->addItem(item);
        this->ui->listWidget->setItemWidget(item, accountItem);
//        accountItem->show();
    }
}



void PasswordManagerWindow::on_pushButton_7_clicked()
{
    // 关闭窗口显示
    hide();
}


void PasswordManagerWindow::on_pushButton_5_clicked()
{
    // 将新增的密码信息进行存储
    QString username = this->ui->input_username->text();
    QString password = this->ui->input_password->text();
    QString domain = this->ui->input_domain->text();

    qDebug() << username << password << domain;
    // 将数据进行加密存储,存储至文件系统中
    std::string crypto = PasswordManager::getInstance().rsaPubEncrypt(password.toStdString());
    qDebug() << "加密完成: " << QString(crypto.c_str());
    password = QString(crypto.c_str());
    // 加入到组件中
    AccountInfo info(domain.toStdString(), username.toStdString(), password.toStdString());
    this->addHostInfo(info);
}


void PasswordManagerWindow::on_pushButton_2_clicked()
{
    QString username = this->ui->username->text();
    QString password = this->ui->password->text();
    QString host = this->ui->host->text();
    qDebug() << "delete";
    // 删除内存中的数据
    std::vector<AccountInfo> _infos;
    for(int i = 0; i < infos.size(); i++){
        if(infos[i].host == host.toStdString() && infos[i].username == username.toStdString())continue;
        _infos.push_back(infos[i]);
    }
    infos = _infos;
    qDebug() << infos.size();
    // 删除存储介质中的数据（文件或者数据库）
    // 清空显示的数据
    resetData();

    // 重新刷新组件
    loadAccountInfo();
}



void PasswordManagerWindow::on_pushButton_4_clicked()
{
    // 查看密码，根据用户输入的密钥进行解密密码信息
    QString key = this->ui->input_key->text();
    qDebug() << "key:" << key;
    QString crypto = this->ui->password->text();
    PasswordManager &instance = PasswordManager::getInstance();
    std::string t2 = instance.rsaPriDecrypt(crypto.toStdString(), instance.readPrivateKey(key.toStdString()));
    if(t2 == ""){
        qDebug() << "解密出错";
        NotifyManager *manager = new NotifyManager();
        manager->setMaxCount(5);
        manager->setDisplayTime(5000);
        manager->setNotifyWndSize(500, 200);
        manager->notify("错误", "解密出错，请检查密钥是否正确");
        return;
    }
    this->ui->password->setText(QString(t2.c_str()));
}


void PasswordManagerWindow::on_pushButton_clicked()
{
    // TODO，复制密码管理器用户名
    qDebug() << "V复制密码管理器用户名";
}


void PasswordManagerWindow::on_pushButton_3_clicked()
{
    // TODO,复制密码管理器密码
    qDebug()<< "复制密码管理器密码";
}


void PasswordManagerWindow::on_pushButton_6_clicked()
{
    QString text = this->ui->newKey->text();
    if(text == "")return;
    PasswordManager::getInstance().generateRSAKey(text.toStdString());
    infos.clear();
    loadAccountInfo();
}

